<?php

/*
 * uri : https://api.worldoftanks.eu/wot/auth/login/
 * https://api.worldoftanks.eu/wot/auth/login/?application_id=d0a293dc77667c9328783d489c8cef73&redirect_uri=http://localhost/wothub/
 * 
 *
 */
class Authentification {    
    private static $appId = array(
                    'RU' => '171745d21f7f98fd8878771da1000a31',
                    'EU' => 'd0a293dc77667c9328783d489c8cef73',
                    'NA' => '16924c431c705523aae25b6f638c54dd',
                    'ASIA' => '39b4939f5f2460b3285bfa708e4b252c',
                    'KR' => 'ffea0f1c3c5f770db09357d94fe6abfb');
    
    public static $uri = 'login.php';
    
    public static function testAuthentification($baseUri) {
        $_SESSION['hub'] = 'EU';
        $_SESSION['appId'] = '90fc54c50076beaadaa6325115fdb2c4';
        if (isset($_SESSION['authentification']) === true) {
            if ($_SESSION['authentification'] === 'ok') {
                return true;
            }
        }
        else {
            if (isset($_GET['status']) === true && $_GET['status'] === 'ok') 
            {
                setcookie('account_id', $_GET['account_id'],mktime(0,0,0,1,1,2025),'/');
                setcookie('access_token', $_GET['access_token'], $_GET['expires_at'],'/');
                setcookie('token_expires', $_GET['expires_at'], $_GET['expires_at'], '/');
                $_SESSION['account_id'] = $_GET['account_id'];
                $_SESSION['access_token'] = $_GET['access_token'];
                $_SESSION['token_expires'] = $_GET['expires_at'];
                return true;
            }
            else {
                if (isset($_COOKIE['account_id']) === true) {
                    $_SESSION['authentification'] = 'ok';
                    $_SESSION['account_id'] = $_COOKIE['account_id'];
                    $_SESSION['access_token'] = $_COOKIE['access_token'];
                    $_SESSION['token_expires'] = $_COOKIE['token_expires'];
                    if ($_SESSION['token_expires'] < time()) {
                        $url = "https://api.worldoftanks.eu/wot/auth/prolongate/?application_id=".$_SESSION['appId']."&access_token=".$_SESSION['access_token'];
                        $token = file_get_contents($url);
                        $json_token = json_decode($token, true);
                        setcookie('access_token', $json_token['data']['access_token'], $json_token['data']['expires_at'],'/');
                        setcookie('token_expires', $json_token['data']['expires_at'], $json_token['data']['expires_at'],'/');
                        $_SESSION['access_token'] = $json_token['data']['access_token'];
                        $_SESSION['token_expires'] = $json_token['data']['expires_at'];
                    }
                    header('Location:'.$baseUri.'Membre/Accueil');
                    return true;
                }
                else {
                   //header('Location:'.$baseUri);
                }
            }            
        }        
        return false;
    }
    
    public static function afficherUri($baseUri) {   
        echo self::$uri;
    }
}